package com.jshoperxms.service.impl;

import com.jshoperxms.controller.mall.backstage.vo.products.ProductSearchResultVo;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.dao.GoodsSpecialContentTDao;
import com.jshoperxms.dao.GoodsSpecialTDao;
import com.jshoperxms.entity.GoodsSpecialContentT;
import com.jshoperxms.entity.GoodsSpecialT;
import com.jshoperxms.service.GoodsSpecialContentTService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 16/10/19.
 * Des:
 */
@Service("goodsSpecialContentTService")
@Scope("prototype")
public class GoodsSpecialContentTServiceImpl extends BaseTServiceImpl<GoodsSpecialContentT> implements GoodsSpecialContentTService {


    @Resource
    private GoodsSpecialContentTDao goodsSpecialContentTDao;
    @Resource
    private GoodsSpecialTDao goodsSpecialTDao;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public void saveGoodsSpecialContentTransactional(GoodsSpecialContentT goodsSpecialContentT) {
        this.goodsSpecialContentTDao.save(goodsSpecialContentT);
        updateGsCount(goodsSpecialContentT);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public void updateGoodsSpecialContentTransactional(GoodsSpecialContentT goodsSpecialContentT) {
        this.goodsSpecialContentTDao.update(goodsSpecialContentT);
        updateGsCount(goodsSpecialContentT);
    }

    private void updateGsCount(GoodsSpecialContentT goodsSpecialContentT){
        List<ProductSearchResultVo> list= GsonJson.parseJsonToData(goodsSpecialContentT.getContent(),ProductSearchResultVo.class);
        if(!list.isEmpty()){
            GoodsSpecialT bean=this.goodsSpecialTDao.findByPK(GoodsSpecialT.class,goodsSpecialContentT.getGoodsSpecialTId());
            if(bean!=null){
                bean.setCount(list.size());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.goodsSpecialTDao.update(bean);
            }
        }
    }
}
